import 'dart:io';

import 'package:sqflite/sqflite.dart';

class DBManager {
  ///数据库版本
  static const int _VERSION = 1;

  ///数据库名称
  static const String _DB_NAME = "gk_chip.db";

  ///数据库实例
  static Database _database;

  static init() async {
    var databasePath = await getDatabasesPath();
    String dbName = _DB_NAME;
    String path = databasePath + "/" + dbName;

    _database = await openDatabase(path,
        version: _VERSION, onCreate: (Database db, int version) async {});
  }

  ///判断指定表是否存在
  static Future<bool> isTableExists(String tabName) async {
    await getCurrentDatabase();
    String sql =
        "select * from sqlite_master where type='table' and name='$tabName'";
    var res = await _database.rawQuery(sql);
    return null != res && res.length > 0;
  }

  ///获取当前数据库实例
  static Future<Database> getCurrentDatabase() async {
    if (null == _database) {
      await init();
    }
    return _database;
  }

  ///关闭数据库
  static close() {
    _database?.close();
    _database = null;
  }
}
